package com.bocai.kelalacar.ui.car.main.presenter;

import android.text.TextUtils;
import android.util.Log;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.base.BaseResponse;
import com.bocai.kelalacar.bean.CodeBean;
import com.bocai.kelalacar.bean.MainListBean;
import com.bocai.kelalacar.bean.PhoneTypeBean;
import com.bocai.kelalacar.bean.UserBean;
import com.bocai.kelalacar.net.Api;
import com.bocai.kelalacar.ui.car.main.contract.MainContract;
import com.bocai.kelalacar.util.AESUtils;
import com.bocai.kelalacar.util.RetryWithDelay;
import com.google.gson.Gson;


import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import cn.jpush.android.api.JPushInterface;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/6/6.
 */

public class MainPresenter extends MainContract.presenter {
    private final MainContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";
    private Subscription subscription;

    @Inject
    public MainPresenter(MainContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }

    @Override
    public void sendCode(String PhoneNumber, String RequestType) {

        api.sendCode(PhoneNumber,RequestType).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    CodeBean bean = new Gson().fromJson(result,CodeBean.class);
                    view.getCode(bean.getCode());
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void getPhoneType(final String PhoneNumber) {
        Log.e("PhoneNumber",PhoneNumber+"");
        api.phoneType(PhoneNumber,"2").subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result =AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    PhoneTypeBean phoneTypeBean = new Gson().fromJson(result,PhoneTypeBean.class);
                    view.getType(phoneTypeBean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void register(String phone, String pw, String code, int type,int show) {
        if (show == 1){
            view.showLoading("");
        }
        if (TextUtils.isEmpty(App.JpushId)){
            App.JpushId = JPushInterface.getRegistrationID(App.getAppContext());
        }
        api.register(phone,pw,code,type,App.JpushId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Log.e("baseResponse",baseResponse.toString());
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    UserBean userBean = new Gson().fromJson(result,UserBean.class);
                    view.getUser(userBean);
                    view.showErrorTip("注册成功，账号已登录");
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void login(String phone, String pw, String MobileDevice,String MemberType) {

        Log.e("login",AESUtils.encode(pw));
        Log.e("login",AESUtils.decode(AESUtils.encode(pw)));

        if (TextUtils.isEmpty(App.JpushId)){
            App.JpushId = JPushInterface.getRegistrationID(App.getAppContext());
        }

        api.login(phone,AESUtils.encode(pw),App.JpushId,MemberType).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result =AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    UserBean userBean = new Gson().fromJson(result,UserBean.class);
                    view.getUser(userBean);

                }else{
                    view.getUser(new UserBean());
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
    @Override
    public void setCode() {
        if (subscription != null){
            subscription.unsubscribe();
        }
        subscription = Observable.interval(1, TimeUnit.SECONDS).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Long aLong) {
                view.setBtnCode((int) (59 - aLong));
                if (aLong == 59) {
                    subscription.unsubscribe();
                }
            }
        });
    }

    @Override
    public void homeList(String pageIndex, String pageSize, String time, String seats, String originCity, String arriveCity) {
        api.homeList(pageIndex,pageSize,time,seats,originCity,arriveCity).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result =AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    MainListBean bean = new Gson().fromJson(result,MainListBean.class);
                    view.getHomeListSuccess(bean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void getUser() {
        view.showLoading("");
        api.getUser().subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    UserBean userBean = new Gson().fromJson(result,UserBean.class);
                    view.getUser(userBean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
}
